package com.dmyang.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.dmyang.common.pojo.BootstrapTableResult;
import com.dmyang.common.pojo.HyResult;
import com.dmyang.dao.HyArticleMapper;
import com.dmyang.entity.HyArticle;
import com.dmyang.entity.HyArticleExample;
import com.dmyang.service.ArticleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 文章管理Controller
 * Created by brss on 2017/8/7.
 */
@Service("articleServiceImpl")
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    private HyArticleMapper articleMapper;

    /**
     * 插入一篇文章（但status属性是由controller传入的）
     * @param article
     * @return
     * @throws Exception
     */
    @Transactional
    @Override
    public HyResult addArticle(HyArticle article) throws Exception {
        article.setWriteTime(new Date());
        article.setPubTime(new Date());
        article.setHits(0);
        //属于那个用户本来是应该传进来的，根据当前登录的用户的id来设置的。
        article.setBelong(1);
        //从Controller出入吧。
        //article.setStatus((byte)1);//1为发布状态，0为草稿状态
        articleMapper.insertSelective(article);
        return HyResult.build(200,"插入成功！");
    }

    /**
     * 根据所属用户，列出所有的文章（保存草稿）
     * @param belong
     * @param offset
     * @param limit
     * @return
     * @throws Exception
     */
    @Transactional
    @Override
    public BootstrapTableResult showArticleList(Integer belong,Integer offset,Integer limit) throws Exception {
        //创建查询条件
        HyArticleExample example = new HyArticleExample();
        HyArticleExample.Criteria criteria = example.createCriteria();
        criteria.andBelongEqualTo(belong);

        //计算开始的页数
        Integer row = (offset/limit) + 1;
        //开始分页
        PageHelper.startPage(row,limit);
        //查询
        List<HyArticle> list = articleMapper.selectByExampleWithBLOBs(example);
        //获取一个有多少条记录
        PageInfo<HyArticle> info = new PageInfo<>(list);
        //打包返回数据
        BootstrapTableResult result = new BootstrapTableResult(info.getTotal(), list);
        return result;
    }

    /**
     * 删除文章（多条用逗号隔开）
     * @param ids
     * @return
     * @throws Exception
     */
    @Transactional
    @Override
    public HyResult deleteArticle(String ids) throws Exception {
        if(ids.contains(",")){
            //删除多条
            String[] split = ids.split(",");
            for (String id : split){
                articleMapper.deleteByPrimaryKey(Integer.parseInt(id));
            }
        }else {
            articleMapper.deleteByPrimaryKey(Integer.parseInt(ids));
        }
        return HyResult.build(200,"删除成功！");
    }

    /**
     * 根据id查询一篇文章
     * @param id
     * @return
     * @throws Exception
     */
    @Transactional
    @Override
    public HyArticle findArticleById(Integer id) throws Exception {
        HyArticle article = articleMapper.selectByPrimaryKey(id);
        return article;
    }

    /**
     * 修改一篇文章
     * @param article
     * @return
     * @throws Exception
     */
    @Transactional
    @Override
    public HyResult modifyArticle(HyArticle article) throws Exception {
        articleMapper.updateByPrimaryKeySelective(article);
        return HyResult.build(200,"修改成功！");
    }

    /**
     * 展示最新的文章
     * @param limit 展示的条数
     * @return
     */
    @Override
    public List<HyArticle> showLastestArticle(Integer limit) {
        List<HyArticle> list = articleMapper.selectByPubTime(limit);
        return list;
    }

    /**
     * 查询categoryId分类下所有的文章
     * @param categoryId
     * @return
     */
    @Override
    public List<HyArticle> showArticleByCate(Integer categoryId) {
        HyArticleExample example = new HyArticleExample();
        HyArticleExample.Criteria criteria = example.createCriteria();
        criteria.andCategoryIdEqualTo(categoryId);
        //查询
        List<HyArticle> articles = articleMapper.selectByExampleWithBLOBs(example);
        return articles;
    }

}
